package com.wxyzz.cem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxyzz.cem.enums.ThreadLocalName;
import com.wxyzz.cem.interceptor.BackAdminInterceptor;
import com.wxyzz.cem.pojo.BackAdmin;
import com.wxyzz.cem.pojo.dto.BackAdminDTO;
import com.wxyzz.cem.service.BackAdminService;
import com.wxyzz.cem.util.Result;
import com.wxyzz.cem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/back-admin")
public class BackAdminController {

    @Autowired
    private BackAdminService backAdminService;
    
    @PostMapping("/register")
    public Result register(@RequestBody BackAdminDTO backAdminDTO) {
        BackAdmin backAdmin = new BackAdmin();
        BeanUtil.copyProperties(backAdminDTO,backAdmin);
        boolean isRegistered = backAdminService.register(backAdmin);
        return Result.success();
    }
    
    @PostMapping("/login")
    public Result<String> login(@RequestBody BackAdminDTO backAdminDTO) {
        BackAdmin backAdmin = new BackAdmin();
        BeanUtil.copyProperties(backAdminDTO,backAdmin);
        String token = backAdminService.login(backAdmin);
        return Result.success(token);
    }

    @PostMapping()
    @BackAdminInterceptor.BackAdminIntercept
    public Result addAdmin(@RequestBody BackAdminDTO backAdminDTO) {
        BackAdmin backAdmin = new BackAdmin();
        BeanUtil.copyProperties(backAdminDTO,backAdmin);
        boolean isAdded = backAdminService.addAdmin(backAdmin);
        return Result.success(isAdded);
    }

    @DeleteMapping()
    @BackAdminInterceptor.BackAdminIntercept
    public Result<Object> batchDelete(@RequestParam List<Integer> backAdminIds) {
        boolean isDeleted = backAdminService.batchDelete(backAdminIds);
        return Result.success(isDeleted);
    }
    @GetMapping("list")
    @BackAdminInterceptor.BackAdminIntercept
    public Result<Page<BackAdmin>> page(@RequestParam(required = false, defaultValue = "1") Integer page,
                       @RequestParam(required = false, defaultValue = "10") Integer size,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) String phone,
                       @RequestParam(required = false) String account
                       )  {
        Page<BackAdmin> backAdminPage = backAdminService.pageAdmin(page, size, name, phone, account);
        return Result.success(backAdminPage);
    }
    
    /**
     * 修改管理员信息
     */
    @PutMapping()
    @BackAdminInterceptor.BackAdminIntercept
    public Result<String> updateAdmin(@RequestBody BackAdminDTO backAdminDTO) {
        BackAdmin adminFromDB = ThreadLocalUtil.get(ThreadLocalName.BACK_ADMIN.getValue(), BackAdmin.class);
        
        BackAdmin adminFromParams = new BackAdmin();
        BeanUtil.copyProperties(backAdminDTO, adminFromParams);
        
        
        adminFromParams.setId(adminFromDB.getId());
        adminFromParams.setAccount(adminFromDB.getAccount());
        
        if (StrUtil.isBlank(adminFromParams.getPassword())) {
            adminFromParams.setPassword(adminFromDB.getPassword());
        } else {
            String encryptedPassword = backAdminService.encryptPassword(adminFromParams);
            adminFromParams.setPassword(encryptedPassword);
        }
        
        return Result.success(backAdminService.updateAdmin(adminFromParams));
    }
}

